package com.bwie.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.User;
import com.bwie.service.UserService;
import com.bwie.mapper.UserMapper;
import com.bwie.utils.R;
import com.bwie.vo.UserVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
* @author lenovo
* @description 针对表【t_user】的数据库操作Service实现
* @createDate 2024-09-03 20:18:47
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{

    @Autowired
    private UserMapper userMapper;


    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * @description：获取验证码
     * @author: sxting~😊
     * @Date:  21:05
     * @param:
     * @param mobile
     * @return: com.bwie.utils.R
    **/
    @Override
    public R sendCode(String mobile) {

        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();

        String redisCode = opsForValue.get("code:" + mobile);

        if(StringUtils.isNotEmpty(redisCode)){
           return R.ERROR(500,"三分钟内不能重复发送");
        }
        Integer num = 4;
        String code = RandomUtil.randomNumbers(num);

        opsForValue.set("code:"+mobile,code,5, TimeUnit.MINUTES);

        return R.OK(code);
    }


    /**
     * @description:验证码登录
     * @author: sxting~😊
     * @Date: 2024/9/3 21:19
     * @param:
     * @param userVo
     * @return: com.bwie.utils.R
    **/
    @Override
    public R codeLogin(UserVo userVo) {

        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();

        String s = opsForValue.get("code:" + userVo.getMobile());

        if(s == null){
            return R.ERROR(500,"验证码已过期");
        }


        QueryWrapper<User> wrapper = new QueryWrapper<>();

        wrapper.lambda().eq(User::getMobile,userVo.getMobile());

        User user = userMapper.selectOne(wrapper);

        if(user == null){
            return R.ERROR(500,"手机号不存在");
        }


        if(!s.equals(userVo.getCode())){
            return R.ERROR(500,"验证码错误");
        }

        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "bwie")
                .claim("id", user.getId())
                .compact();

        stringRedisTemplate.opsForValue().set("token:"+user.getId(),token,30,TimeUnit.MINUTES);

        user.setToken(token);

        return R.OK(user);
    }

    @Override
    public R findPassword(UserVo userVo) {


        ValueOperations<String, String> opsForValue = stringRedisTemplate.opsForValue();

        String s = opsForValue.get("code:" + userVo.getMobile());

        if(s == null){
            return R.ERROR(500,"验证码已过期");
        }

        QueryWrapper<User> wrapper = new QueryWrapper<>();

        wrapper.lambda().eq(User::getMobile,userVo.getMobile());

        User user = userMapper.selectOne(wrapper);

        if(user == null){
            return R.ERROR(500,"手机号不存在");
        }

        if(!s.equals(userVo.getCode())){
            return R.ERROR(500,"验证码错误");
        }

        return R.OK(user);
    }

    @Override
    public R updatePassword(UserVo userVo) {
        R r = userMapper.updatePassword(userVo);
        return R.OK(r);
    }


    /**
     * @description:用户密码登录
     * @author: sxting~😊
     * @Date: 2024/9/3 21:06
     * @param:
     * @param userVo
     * @return: com.bwie.utils.R
    **/
    @Override
    public R login(UserVo userVo) {

        QueryWrapper<User> wrapper = new QueryWrapper<>();

        wrapper.lambda().eq(User::getUsername,userVo.getUsername());

        User userLogin = userMapper.selectOne(wrapper);

        if(userLogin == null){
            return R.ERROR(500,"用户名不存在");
        }

        String bcrypt = DigestUtil.bcrypt(userLogin.getPassword());
        System.out.println("加密的密码："+bcrypt);

        boolean b = DigestUtil.bcryptCheck(userVo.getPassword(), userLogin.getPassword());

        if(!b){
            return R.ERROR(500,"密码错误");
        }

        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "bwie")
                .claim("id", userLogin.getId())
                .compact();

        stringRedisTemplate.opsForValue().set("token:"+userLogin.getId(),token,60,TimeUnit.MINUTES);

        userLogin.setToken(token);

        return R.OK(userLogin);
    }
}




